package cn.edu.leetcode.查找;

public class 搜索旋转排序数组 {
    public static void main(String[] args) {
        int[] nums = {1, 3};
        int target = 3;

        System.err.println(search(nums, target));
    }

    public static int search(int[] nums, int target) {
        int len = nums.length;
        if(len == 0) return -1;
        int left = 0, right = len - 1;

        while(left <= right){

            int mid = left + (right - left) / 2;
            if(nums[mid] == target) return mid;

            // 右边有序
            if(nums[mid] < nums[right]){
                // 目标值在右边
                if(target > nums[mid] && target <= nums[right]){
                    left = mid + 1;
                // 目标值在左边
                }else{
                    right = mid - 1;
                }
            // 左边有序
            }else{
                // 目标值在左边
                if(target >= nums[left] && target < nums[mid]){
                    right = mid - 1;
                    // 目标值在右边
                }else{
                    left = mid + 1;
                }
            }
        }
        return -1;
    }

}
